package com.ruoyi.mdm.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mdm.domain.SkumInfoEntity;
import com.ruoyi.mdm.repository.SkumInfoRepository;
import com.ruoyi.mdm.service.ISkumInfoService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.client.erhlc.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.client.erhlc.NativeSearchQuery;
import org.springframework.data.elasticsearch.client.erhlc.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@Service
public class SkumInfoServiceImpl implements ISkumInfoService {

    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Resource
    private SkumInfoRepository skumInfoRepository;

    @Override
    public SearchHits<SkumInfoEntity> searchSkumInfo(SkumInfoEntity skumInfoEntity) {

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        // 关键字模糊检索
        if (StringUtils.isNotEmpty(skumInfoEntity.getKeyword())) {
            boolQueryBuilder.must(QueryBuilders.matchPhrasePrefixQuery("keyword", skumInfoEntity.getKeyword()));
//            boolQueryBuilder.must(QueryBuilders.multiMatchQuery(skumInfoEntity.getKeyword(),
//                    "skum_name", "skum_name_en", "skum_short_name"));
        }

//        // 品牌编码
//        if (StringUtils.isNotEmpty(skumInfoEntity.getOwnerId())) {
//            boolQueryBuilder.must(QueryBuilders.termQuery("owner_id", skumInfoEntity.getOwnerId()));
//        }
        // 品牌编码 多个时候 或者
        if (StringUtils.isNotEmpty(skumInfoEntity.getOwnerId())) {
            List<String> ownerIdList = Arrays.asList(skumInfoEntity.getOwnerId().split(","));
            BoolQueryBuilder boolOwnerBuilder = QueryBuilders.boolQuery();
            for (String ownerId : ownerIdList) {
                boolOwnerBuilder.should(QueryBuilders.termQuery("owner_id", ownerId));
            }
            boolOwnerBuilder.minimumShouldMatch(1);

            boolQueryBuilder.must(boolOwnerBuilder);
        }

        // 产品价格 精确检索
        if (skumInfoEntity.getSkumSalePrice() != null) {
            boolQueryBuilder.must(QueryBuilders.termQuery("skum_sale_price", skumInfoEntity.getSkumSalePrice()));
        }

        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        // 条件
        nativeSearchQueryBuilder.withQuery(boolQueryBuilder);
        // 命中得分排序
        if (StringUtils.isNotEmpty(skumInfoEntity.getKeyword())) {
            nativeSearchQueryBuilder.withSort(SortBuilders.scoreSort().order(SortOrder.DESC));
        }
        // 再更新时间排序
        nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("update_time.keyword").order(SortOrder.DESC));
        // 分页
        nativeSearchQueryBuilder.withPageable(PageRequest.of(skumInfoEntity.getPage(), skumInfoEntity.getSize()));

        NativeSearchQuery nativeSearchQuery = nativeSearchQueryBuilder.build();

        return elasticsearchRestTemplate.search(nativeSearchQuery, SkumInfoEntity.class);
    }

    @Override
    public SkumInfoEntity findById(Long id) {
        Optional<SkumInfoEntity> optionalSkumInfoEntity = skumInfoRepository.findById(id);
        return optionalSkumInfoEntity.get();
    }

    @Override
    public int add(SkumInfoEntity skumInfoEntity) {
        skumInfoRepository.save(skumInfoEntity);
        return 1;
    }

    @Override
    public int addAll(List<SkumInfoEntity> skumInfoEntityList) {
        skumInfoRepository.saveAll(skumInfoEntityList);
        return 1;
    }

    @Override
    public int delete(Long id) {
        skumInfoRepository.deleteById(id);
        return 1;
    }

    @Override
    public int deleteAll() {
        skumInfoRepository.deleteAll();
        return 1;
    }

}
